package com.zego.wawaji.ui.coupon.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ListView;

import com.zego.wawaji.R;
import com.zego.wawaji.ui.base.BaseDialogFragment;
import com.zego.wawaji.ui.bean.pay.Pay;
import com.zego.wawaji.ui.coupon.bean.Coupon;
import com.zego.wawaji.ui.dialog.PickerDialogUtil;
import com.zego.wawaji.wawaji_client.AppTinkerLike;

import java.util.List;

/**
 * 充值时优惠券选择弹框
 * Created by Su on 2018/2/26.
 */
@SuppressLint("ValidFragment")
public class CouponBottomDialog extends BaseDialogFragment {
    private Context mContext;
    private View dialogView;
    private Dialog dialog;
    private ListView lv_defriend;

    private CouponBottimAdapter mAdapter;
    private List<Coupon> couponList;  // 优惠券列表
    private Coupon couponData;           // 选中的优惠券
    private Pay pay;                // 充值信息

    public CouponBottomDialog() {
        super();
    }

    public CouponBottomDialog(Context mContext, List<Coupon> list, Pay pay) {
        this.mContext = mContext;
        this.pay = pay;
        this.couponList = list;
        initDialogView();
        initBottomDialog();
        initData();
    }

    private void initDialogView() {
        dialogView = View.inflate(mContext, R.layout.coupon_bottom_dialog, null);
        lv_defriend = (ListView) dialogView.findViewById(R.id.lv_defriend);
        dialogView.findViewById(R.id.de_cancel).setOnClickListener(v -> dismissDialog());

        dialogView.findViewById(R.id.btn_submit).setOnClickListener(v -> {
            dismissDialog();
            PickerDialogUtil.showPayCouponDialog(AppTinkerLike.getActivity(), pay, couponData);
        });
    }

    private void initBottomDialog() {
        dialog = new Dialog(mContext);
        dialog.setContentView(dialogView);
        dialog.setCanceledOnTouchOutside(true);
        Window window = dialog.getWindow();
        window.setBackgroundDrawableResource(R.color.transparent);
        WindowManager.LayoutParams params = window.getAttributes();
        params.dimAmount = 0.1f;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.AnimDownInDownOut);
    }

    public void showDialog() {
        if (dialog != null) {
            dialog.show();
        }
    }

    public void dismissDialog() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    private void initData() {
        mAdapter = new CouponBottimAdapter(mContext, couponList);
        lv_defriend.setAdapter(mAdapter);
        mAdapter.setOnChooseListener((coupon, position) -> couponData = coupon);
    }
}
